package com.jdk.demo.io.so_lingerDemo.demo2;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {

	public static void main(String[] args) throws Exception {
		Socket socket = new Socket();
		// 设置超小的接收缓冲区
		// 目的是先让Server服务端发送close
		// 然后将服务端发送缓冲区中的数据再传入客户端的接收缓冲区中
		// 虽然服务端close方法已经执行，但是数据不会丢失
		socket.setReceiveBufferSize(1);
		socket.bind(new InetSocketAddress("localhost", 7077));
		socket.connect(new InetSocketAddress("localhost", 8088));

		InputStream inputStream = socket.getInputStream();
		byte[] bytearray= new byte[1];
		int readLength = inputStream.read(bytearray);
		while(readLength != -1) {
			System.out.println(new String(bytearray, 0, readLength));
			readLength = inputStream.read(bytearray);
		}
		System.out.println("client read end time=" + System.currentTimeMillis());
		inputStream.close();
		socket.close();
	}

}
